/*
 * @Author: xiaosihan
 * @Date: 2021-04-18 10:08:38
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2021-11-07 14:31:46
 */



import _ from "lodash";
import * as THREE from "three";
import { Material } from "three";
import Object3D from "../Object3D/Object3D";
import Object3DComponent, { Object3DComponentProps } from "../Object3D/Object3DComponent";
import fontContorller from "../Text/fontController";

interface MeshSeatProps extends Object3DComponentProps {
    text?: string;
}

export default class MeshSeat extends Object3DComponent<MeshSeatProps> {
    constructor(props: MeshSeatProps) {
        super(props);

        this.object3d = new THREE.Mesh() as unknown as Object3D & THREE.Mesh;

        if (_.get(this.object3d.material, ["isMaterial"])) {
            this.object3d.material.side = THREE.DoubleSide;
        }
    }

    declare object3d: Object3D & THREE.Mesh;

    componentDidMount() {
        //设置文字
        this.setGeometry();
    }

    componentDidUpdate() {
        //设置文字
        this.setGeometry();
    }

    setGeometry() {
        const { text = "<MeshSeat/>" } = this.props;

        // 参数都没有变时 无需重复创建 文字模型
        if (_.isEqual({ text }, this.object3d.geometry.userData)) {
            return;
        }

        // 加载文字
        fontContorller.loadText({
            fontType: "simhei",
            text: text,
            callback: THREEFont => {
                this.object3d.geometry.dispose();
                this.object3d.geometry = new THREE.ShapeBufferGeometry(THREEFont.generateShapes(text, 2), 12);
                this.object3d.geometry.userData = { text }
                this.object3d.geometry.center();
            }
        });

    }

}